package com.example.logistics.controller;

import com.example.logistics.entity.Warehouse;
import com.example.logistics.repository.WarehouseRepository;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/warehouses")
@RequiredArgsConstructor
public class WarehouseController {
    private final WarehouseRepository warehouseRepository;

    @GetMapping
    public List<Warehouse> list() {
        return warehouseRepository.findAll();
    }

    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public Warehouse create(@Valid @RequestBody Warehouse warehouse) {
        return warehouseRepository.save(warehouse);
    }

    @PutMapping("/{id}")
    public Warehouse update(@PathVariable Long id, @Valid @RequestBody Warehouse body) {
        Warehouse w = warehouseRepository.findById(id).orElseThrow();
        w.setName(body.getName());
        w.setLocation(body.getLocation());
        w.setCapacity(body.getCapacity());
        return warehouseRepository.save(w);
    }

    @DeleteMapping("/{id}")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void delete(@PathVariable Long id) {
        warehouseRepository.deleteById(id);
    }
}


